// featurePackage/pages/giftCardBind/giftCardBind.js
import {
  giftBind
} from "../../../service/jb_gift_service"
import {
  getSmsCode
} from "../../../service/jb_common_service"
import debounce from "../../../tools/debounce"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      cardNo: '',
      smsCode: "",
    },
    couponCodeBtnStatus: false,
    couponCodeBtnTitle: "获取验证码",
    snsMsgWait: 60,//倒计时
    userPhone: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userPhone: app.globalData.phone
    })
  },
  



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.giftCardNo){
      this.setData({
        'formData.cardNo':app.globalData.giftCardNo
      })
      app.globalData.giftCardNo = null
    }
  },



  bindCardAction() {
    let rData = this.data.formData;
    if (!rData.cardNo.trim()) {
      wx.showToast({
        title: '请输入卡号',
        icon: "none"
      })
      return
    }
    // if (!rData.phone.trim() || rData.phone.length != 11) {
    //   wx.showToast({
    //     title: '请输入正确的手机号',
    //     icon: "none"
    //   })
    //   return
    // }
    if (!rData.smsCode.trim()) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return
    }

    debounce(this.queryBindCard);
  },

  //确认绑定
  queryBindCard() {
    wx.showLoading();
    giftBind(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '绑卡成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '绑定失败',
        icon: "none"
      })
      console.error('JJLog:giftBind', err);
    })
  },

  //输入变化绑定:
  cellInputChange(e) {
    // console.log("JJLog:输入变化:" + JSON.stringify(e));
    let value = e.detail.value;
    let key = e.currentTarget.dataset.key;
    let changeValue = `formData.${key}`
    this.setData({
      [changeValue]: value
    })

  },

  //发送验证码
  clickSendCode() {

    if (this.data.couponCodeBtnStatus) {
      return;
    }
    let that = this;
    //点击了发送按钮，就先禁用掉这个按钮点击
    this.setData({
      couponCodeBtnStatus: true,
    })
    //调用发送验证码接口:
    let phone = this.data.userPhone;
    let warn = null;
    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      getSmsCode(phone).then((data) => {
        //当手机号正确的时候提示用户短信验证码已经发送
        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });
        let inter = setInterval(function () {
          this.setData({
            couponCodeBtnStatus: true,
            couponCodeBtnTitle: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              couponCodeBtnTitle: '重新发送',
              snsMsgWait: 60,
              couponCodeBtnStatus: false
            });
          }
        }.bind(this), 1000);

      }).
        catch((err) => {
          that.setData({
            couponCodeBtnStatus: false
          })
          wx.showToast({
            icon: 'none',
            title: err,
          })
        })


    }
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        couponCodeBtnStatus: false
      })
      return;
    }


  },


})